package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.Account;
import com.meichis.yndms.repository.ApiUtil;

import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class AcountManager extends BaseManager {

    private static AcountManager INSTANCE;

    public static AcountManager getInstance() {
        if (INSTANCE == null) {
            synchronized (AcountManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AcountManager();
                }
            }
        }
        return INSTANCE;
    }

    public void GetCurrentAccount(final MutableLiveData<Boolean> showDialog, String IBMDealerID, MutableLiveData<Account> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("IBMDealerID", IBMDealerID);
        getRootApi().GetCurrentAccount(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<Account>>() {
                }.getType(), new IWebServiceCallback<List<Account>>() {
                    @Override
                    public void onSucceed(int sequence,List<Account> resultPack, String ReturnInfo, int Return) {
                        if (resultPack != null && resultPack.size() > 0) {
                            data.setValue(resultPack.get(0));
                        } else {
                            data.setValue(new Account());
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence,int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

}
